<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$intID = (int)$_data["ID"];

$ST = DateForm(date("Y-m-d"), date("H:i:s"), "Y-m-d H:i:s");
$arrG = getGambleDataByID($intID);

if($arrG === false){
	ErrMsgOutputHTML("Error1", "", 1);
}elseif(sizeof($arrG) == 0 || ($arrG["isEnd"] == 1 && $arrG["PayupDate"] < date("Y-m-d"))){//$ST
	header("Location: ".basename($_SERVER["PHP_SELF"])."?op=List&GameDate=".$_data["GameDate"]);
	exit();
//開獎時間未到，無法結帳
}elseif($arrG["GameDate"]." ".$arrG["GameTime"] > date("Y-m-d H:i:s")){
	$GLOBALS["errormessage"] = "開獎時間未到，無法結帳!!";
	ErrMsgOutputHTML("Error2", "", 1);
}

$arrLotto = GetLottoData($arrG["LottoID"]);
if(!$arrLotto)ErrMsgOutputHTML("Error2", "", 1);

$_data["NumberSpecial"] = intval(trim($_data["NumberSpecial"]));
$_data["NumberNormal0"] = intval(trim($_data["NumberNormal0"]));
$_data["NumberNormal1"] = intval(trim($_data["NumberNormal1"]));
$_data["NumberNormal2"] = intval(trim($_data["NumberNormal2"]));
$_data["NumberNormal3"] = intval(trim($_data["NumberNormal3"]));
$_data["NumberNormal4"] = intval(trim($_data["NumberNormal4"]));
$_data["NumberNormal5"] = intval(trim($_data["NumberNormal5"]));
$_data["NumberNormal6"] = intval(trim($_data["NumberNormal6"]));

$isErr = true;

switch($arrLotto["LType"]){
	case 1:
		if(!($_data["NumberSpecial"] > 0 && $_data["NumberSpecial"] < 50)){
			$GLOBALS["errormessage"] = "Special Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal0"] > 0 && $_data["NumberNormal0"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal1"] > 0 && $_data["NumberNormal1"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal2"] > 0 && $_data["NumberNormal2"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal3"] > 0 && $_data["NumberNormal3"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal4"] > 0 && $_data["NumberNormal4"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal5"] > 0 && $_data["NumberNormal5"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}else{
			$isErr = false;
		}
		$_data["NumberNormal"] = $_data["NumberNormal0"].",".$_data["NumberNormal1"].",".$_data["NumberNormal2"].",".$_data["NumberNormal3"].",".$_data["NumberNormal4"].",".$_data["NumberNormal5"];
		break;

	case 2:
		if(!($_data["NumberNormal0"] > -1 && $_data["NumberNormal0"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal1"] > -1 && $_data["NumberNormal1"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal2"] > -1 && $_data["NumberNormal2"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}else{
			//if($arrG["LottoID"] == 7 || $arrG["LottoID"] == 8){
			//	if(!($_data["NumberNormal3"] > -1 && $_data["NumberNormal3"] < 10)){
			//		$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
			//	}elseif(!($_data["NumberNormal4"] > -1 && $_data["NumberNormal4"] < 10)){
			//		$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
			//	}else{
			//		$isErr = false;
			//	}
			//}else{
				$isErr = false;
			//}
		}
		//if($arrG["LottoID"] == 7 || $arrG["LottoID"] == 8){
		//	$_data["NumberNormal"] = $_data["NumberNormal0"].",".$_data["NumberNormal1"].",".$_data["NumberNormal2"].",".$_data["NumberNormal3"].",".$_data["NumberNormal4"];
		//}else{
			$_data["NumberNormal"] = $_data["NumberNormal0"].",".$_data["NumberNormal1"].",".$_data["NumberNormal2"];
		//}
		break;

	case 3:
		if(!($_data["NumberNormal0"] > -1 && $_data["NumberNormal0"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal1"] > -1 && $_data["NumberNormal1"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal2"] > -1 && $_data["NumberNormal2"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal3"] > -1 && $_data["NumberNormal3"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal4"] > -1 && $_data["NumberNormal4"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal5"] > -1 && $_data["NumberNormal5"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal6"] > -1 && $_data["NumberNormal6"] < 10)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}else{
			$isErr = false;
		}
		$_data["NumberNormal"] = $_data["NumberNormal0"].",".$_data["NumberNormal1"].",".$_data["NumberNormal2"].",".$_data["NumberNormal3"].",".$_data["NumberNormal4"].",".$_data["NumberNormal5"].",".$_data["NumberNormal6"];
		break;

	case 4:
		if(!($_data["NumberSpecial"] > 0 && $_data["NumberSpecial"] < 50)){
			$GLOBALS["errormessage"] = "Special Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal0"] > 0 && $_data["NumberNormal0"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal1"] > 0 && $_data["NumberNormal1"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal2"] > 0 && $_data["NumberNormal2"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}elseif(!($_data["NumberNormal3"] > 0 && $_data["NumberNormal3"] < 50)){
			$GLOBALS["errormessage"] = "Normal Number Data is Error Data!!";
		}else{
			$isErr = false;
		}
		$_data["NumberNormal"] = $_data["NumberNormal0"].",".$_data["NumberNormal1"].",".$_data["NumberNormal2"].",".$_data["NumberNormal3"];
		break;
} //if($arrLotto["LType"] == 1)

if($isErr)ErrMsgOutputHTML("Error3", "", 1);

//設置結帳旗標
setFlagToDB('OnWagerReult', 15);

//更改樂透中獎號碼
$strSQL = "update `bet_GambleScore` set NumberNormal = '".$_data["NumberNormal"]."', NumberSpecial = ".$_data["NumberSpecial"].", AddDate = CURRENT_DATE(), AddTime = CURRENT_TIME() where GambleID = ".$intID;
if(!PMA_mysql_query($strSQL, $_ConnMaster)){
	$GLOBALS["errormessage"] = "Import Score Error!!";
	$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	//移除結帳旗標
	delFlagToDB('OnWagerReult');
	ErrMsgOutputHTML("Error4", "", 1);
}

//更改樂透中獎號碼
if(mysql_affected_rows($_ConnMaster) == 0){
	$strSQL = "insert into `bet_GambleScore`(GambleID, NumberNormal, NumberSpecial, AddDate, AddTime ) values('".$intID."','".$_data["NumberNormal"]."','".$_data["NumberSpecial"]."',CURRENT_DATE(),CURRENT_TIME())";

	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		$GLOBALS["errormessage"] = "Import Score Error!!";
		$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
		//移除結帳旗標
		delFlagToDB('OnWagerReult');
		ErrMsgOutputHTML("Error5", "", 1);
	}
}
	
//$_data["LottoID"] = trim($_data["LottoID"]);
//計算賽果
/*$strSQL = "insert into `bet_BetControl`(`BetKey`) values('OnWagerReult')";
$strSQLRm = "delete from `bet_BetControl` where `BetKey` = 'OnWagerReult'";
while (!PMA_mysql_query($strSQL, $_ConnMaster)) {
	if($i < 15){
		$i++;
		sleep(4);
	}else{
		@PMA_mysql_query($strSQLRm, $_ConnMaster);
	}
}*/
//sleep(30);
if(!CalculateWager($_arrLotto[$arrG["LottoID"]], $intID)){
	//@rmdir($_strLockFile);
	//移除結帳旗標
	delFlagToDB('OnWagerReult');
	ErrMsgOutputHTML("Error6", "", 1);
}

$strSQL = "update `bet_BetListTmp` t1  inner join  `bet_BetList` t2 on t1.ID = t2.ID set t1.MemResult = t2.MemResult where t1.GambleID = '".$intID."' ";
if(!PMA_mysql_query($strSQL, $_ConnMaster)){
	$GLOBALS["errormessage"] = "Import Result Error!!";
	$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	//移除結帳旗標
	delFlagToDB('OnWagerReult');
	ErrMsgOutputHTML("Error8", "", 1);
}

switch($_blDBName){
	case "betNewLotto":
	case "betUnion168netUTF8":
	case "betFiveUBestusUTF8":
	case "betLonUBestusUTF8":
		$strSQL = "delete from `bet_Layer1Report` where `GambleID` = '".$intID."' ";
		@PMA_mysql_query($strSQL, $_ConnMaster);

		$strSQL = "INSERT INTO `bet_Layer1Report`(
			  LottoID, GambleID, Bet_Type, PayupDate, WagerType, 
			  MemID, MemExchangeRate, MemCurrency, wgNo, MemStake, MemPayOff, MemResult, MemCommission,
			  SubID, SubIncome, SubResult, SubRate, SubRateResult, SubCommission,
			  MainID, MainResult, MainRate, MainRateResult, MainCommission,
			  StockID, StockResult, StockRate, StockRateResult, StockCommission,
			  DirectorID, DirectorResult, DirectorRate, DirectorRateResult,
			  DirectorCommission
		)
		select 
			  LottoID, GambleID, Bet_Type, PayupDate, WagerType, 
			  MemID, MemExchangeRate, MemCurrency, COUNT(*), SUM(MemStake), SUM(MemPayOff), SUM(MemResult), SUM(MemCommission*MemExchangeRate*MemStake),
			  SubID, SUM(SubIncome), SUM(SubResult), SubRate, SUM(SubRateResult), SUM(SubCommission*SubRate*MemExchangeRate*MemStake),
			  MainID, SUM(MainResult), MainRate, SUM(MainRateResult), SUM(MainCommission*MainRate*MemExchangeRate*MemStake),
			  StockID, SUM(StockResult), StockRate, SUM(StockRateResult), SUM(StockCommission*StockRate*MemExchangeRate*MemStake),
			  DirectorID, SUM(DirectorResult), DirectorRate, SUM(DirectorRateResult),
			  SUM(DirectorCommission*DirectorRate*MemExchangeRate*MemStake)
		from `bet_BetList` where `isPayOff` = 0 and `GambleID` = '".$intID."' group by LottoID,  GambleID, Bet_Type, PayupDate, WagerType, MemID
		ON DUPLICATE KEY UPDATE 
			  wgNo = VALUES(wgNo), MemStake = VALUES(MemStake), 
			  MemPayOff = VALUES(MemPayOff), 
			  MemResult = VALUES(MemResult), 
			  SubIncome = VALUES(SubIncome), 
			  SubResult = VALUES(SubResult), 
			  SubRateResult = VALUES(SubRateResult), 
			  MainResult = VALUES(MainResult), 
			  MainRateResult = VALUES(MainRateResult), 
			  StockResult = VALUES(StockResult), 
			  StockRateResult = VALUES(StockRateResult), 
			  DirectorResult = VALUES(DirectorResult), 
			  DirectorRateResult = VALUES(DirectorRateResult);";
		@PMA_mysql_query($strSQL, $_ConnMaster);
		break;
}

//移除結帳旗標
delFlagToDB('OnWagerReult');
//@PMA_mysql_query($strSQLRm, $_ConnMaster);

//紀錄輸入號碼紀錄
$strLog = "序號：".$intID."\n";
$strLog .= "LID：".$arrG["LottoID"]."\n";
$strLog .= "正碼：".$_data["NumberNormal"]."\n";
$strLog .= "特碼：".$_data["NumberSpecial"]."\n";
AddData_Log($_SESSION["Admin_Info"] -> ID, "輸入樂透中獎號碼", $strLog);

//if(!DeleteTodayGame($intID, $_data["LottoID"])){
//	ErrMsgOutputHTML("Error", "", 1);
//}

//將此期樂透狀態，改為已結束
$strSQL = "update `bet_Gamble` set isEnd = '1' where ID = ".$intID;
if(!PMA_mysql_query($strSQL, $_ConnMaster)){
	$GLOBALS["errormessage"] = "End This Game Error!!";
	$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	ErrMsgOutputHTML("Error7", "", 1);
}

if(!MakeGambleFile($_data["LottoID"]))ErrMsgOutputHTML("Error8", "", 1);
for($i = 1; $i < sizeof($_arrBet_Type); $i++){
	if(!MakeTodayGameFile($_data["LottoID"], $i))ErrMsgOutputHTML("Error9", "", 1);
}
if(!getGameScoreFile($_data["LottoID"]))ErrMsgOutputHTML("Error10", "", 1);

$strErrTitle = $_strCompetition["CountResult_Title"];
$GLOBALS["errormessage"] = $_strCompetition["CountResult_Tip".$arrLotto["LType"]];
$GLOBALS["errormessage"] = str_replace("{NumberNormal}", $_data["NumberNormal"], $GLOBALS["errormessage"]);
$GLOBALS["errormessage"] = str_replace("{NumberSpecial}", $_data["NumberSpecial"], $GLOBALS["errormessage"]);
$isNotAlert = true;
ErrMsgOutputHTML(" Finish ", "", 1);
?>